<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Auth\JwtToken;
use Closure;

class Token
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $authorization = $request->header('Authorization');
        if(strlen($authorization)<1){
            return response()->json('token error',401);
        }
        $token = explode(" ",$authorization);
        try{
            $token = $token[1];
        }catch (\Exception $e){
            return response()->json('token error',401);
        }
        $user = JwtToken::verifyToken($token);
        $request->is_auth_user = true;
        $request->user_id = $user['id'];
        $request->user_type = $user['user_type'];
        return $next($request);
    }
}
